public class Test {

      public class TreeNode {
          int val;
          TreeNode left;
          TreeNode right;
          TreeNode() {}
          TreeNode(int val) { this.val = val; }
          TreeNode(int val, TreeNode left, TreeNode right) {
              this.val = val;
              this.left = left;
              this.right = right;
          }
      }

    class Solution {
        //获取节点的高度
        int getHeight(TreeNode root){
            if(root == null ) return 0;

            int leftHeight =  getHeight(root.left);
            int rightHeight = getHeight(root.right);
            //这里求过程中每一个子树的高度，一旦有不平衡的就返回-1
            //(leftHeight >= 0 && rightHeight >= 0是因为左右两边都返回-1
            if(leftHeight >= 0 && rightHeight >= 0 && Math.abs(leftHeight - rightHeight)<=1){
                return Math.max(leftHeight,rightHeight)+1;
            }else{
                return -1;
            }
        }

        public boolean isBalanced(TreeNode root){
         if(root == null ) return true;
            return getHeight(root) >0;
        }


    }



}
